<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
class Product extends Base{
    //产品搜索
    public function prosearch(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.key"))){
            $key=input('param.key');
        }else{
            return json(['code' => 503, 'msg' => '搜索关键词呢']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('product')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('product')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $products=Db::name('product')->where('cus_id',$cus_id)->where('state',1)->where('title','like',"%{$key}%")->order('id desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','key'=>$key,'data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //轮播图
    public function banner(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $banner=Db::name('banner')->where('cus_id',$cus_id)->where('state',1)->where('mtype',1)->order('sort desc')->select();
                if($banner){
                    foreach ($banner as $key => $value) {
                        $banner[$key]['time']=date('Y-m-d H:i:s',$banner[$key]['time']);
                        $banner[$key]['uptime']=date('Y-m-d H:i:s',$banner[$key]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$banner]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $banner=Db::name('banner')->where('cus_id',$cus_id)->where('state',1)->where('mtype',1)->order('sort desc')->select();
                if($banner){
                    foreach ($banner as $key => $value) {
                        $banner[$key]['time']=date('Y-m-d H:i:s',$banner[$key]['time']);
                        $banner[$key]['uptime']=date('Y-m-d H:i:s',$banner[$key]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$banner]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $banner=Db::name('banner')->where('cus_id',$cus_id)->where('state',1)->where('mtype',1)->order('sort desc')->select();
                if($banner){
                    foreach ($banner as $key => $value) {
                        $banner[$key]['time']=date('Y-m-d H:i:s',$banner[$key]['time']);
                        $banner[$key]['uptime']=date('Y-m-d H:i:s',$banner[$key]['uptime']);
                    }
                    return json(['code' => 200, 'msg' => '成功！','data'=>$banner]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //产品所有对应的产品--分页
    public function productalls(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 503, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('product')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('product')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $products=Db::name('product')->where('cus_id',$cus_id)->where('state',1)->order('id desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //产品分类名对应的产品--分页
    public function products(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        if(!empty(input("param.cate_id"))){
            $cate_id=input("param.cate_id");
        }else{
            return json(['code' => 503, 'msg' => '请给出分类名的id']);exit();
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 504, 'msg' => '当前分页数']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('product')->where('cate_id',$cate_id)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('product')->where('cate_id',$cate_id)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $products=Db::name('product')->where('cate_id',$cate_id)->where('cus_id',$cus_id)->where('state',1)->order('sort desc')->limit(4*($page-1),4)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
    //产品推荐
    public function reproducts(){
        if(!empty(input("param.cus_id"))){
            $cus_id=input("param.cus_id");
        }else{
            return json(['code' => 501, 'msg' => '请给出客户id']);exit();
        }
        $customer=Db::name('customer')->where('id',$cus_id)->where('state',1)->find();
        if($customer){
            if(input("param.platform")==1&&$customer['platformwx_state']==1){
                $products=Db::name('product')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('id desc')->limit(3)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==2&&$customer['platformbd_state']==1){
                $products=Db::name('product')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('id desc')->limit(3)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }elseif(input("param.platform")==3&&$customer['platformtt_state']==1){
                $products=Db::name('product')->where('cus_id',$cus_id)->where('state',1)->where('flag',1)->order('id desc')->limit(3)->select();
                if($products){
                    return json(['code' => 200, 'msg' => '成功！','data'=>$products]);exit();
                }else{
                    return json(['code' => 500, 'msg' => '查无数据！']);exit();
                }
            }else{
                return json(['code' => 500, 'msg' => '查无数据！']);exit();
            }
        }else{
            return json(['code' => 502, 'msg' => '此客户不存在或者被禁用了！']);exit();
        }
    }
}